<?php


namespace app\common\model;


use think\Exception;
use XznsTools\cache\CacheManage;

class ZlUserAttention extends BaseCache
{
    protected $table = 'zl_user_attention';

    protected $cacheModel = 'user_attention';

    /**
     * 和占星师的关注状态
     * @param $uid
     * @param $astrUid
     * @return int
     * @throws \Exception
     */
    public function andAstrStatusOfAttention($uid, $astrUid)
    {
        // 获取我关注的用户id
        $myAttentionUid = $this->getAttentionUid($uid);
        if (in_array($astrUid, $myAttentionUid)) // 判断我是否关注了当前占星师
        {   // 获取占星师关注的用户id
            $astrAttentionUid = $this->getAttentionUid($astrUid); // 判断占星师是否关注了我
            if (in_array($uid, $astrAttentionUid))
            {
                return 2; // 已互相关注
            } else
            {
                return 1; // 已关注
            }
        } else
        {
            return 0; // 未关注
        }
    }

    /**
     * 缓存获取用户关注占星师uid
     * @param $uid
     * @param bool $isReturnTime
     * @return array|mixed
     * @throws \Exception
     */
    public function getAttentionUid($uid, $isReturnTime = false)
    {
        $isReturnTime = $isReturnTime ? true : false;

        $attention = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid)
            ->ZsGetIocData('list', function () use ($uid)
            {
                return $this->getUserAttentionByDb($uid);
            }, $isReturnTime);

        return $attention;
    }

    /**
     * 从数据库获取用户关注占星师uid
     * @param $uid
     * @return array
     */
    public function getUserAttentionByDb($uid)
    {
        try
        {
            $data = $this->where('uid', $uid)->column('create_time', 'attention_uid');
        }
        catch (Exception $e)
        {
            return [];
        }

        return $data;
    }
}
